package mario.game.graymachine.sprite;

import mario.game.graymachine.R;
import mario.game.graymachine.core.Scene;

public class Fly extends GLSprite {
	int hp = 100;
	static final int ARRAY_LENGTH=5;
	public Fly(Scene scene) {
		
		super(scene);
		type=GLSprite.TYPE_ENIMY;
		
		mResourceIds = new int[5][ARRAY_LENGTH];
		/** date|date|date|timer|number */
		mTextureNames = new int[5][ARRAY_LENGTH+2];//more 2 grid to store timeindex and image numbers
		
		// resouce id
		mTextureNames[1][ARRAY_LENGTH+1]=4;
		mResourceIds[1][0]=R.drawable.fly1;
		mResourceIds[1][1]=R.drawable.fly2;
		mResourceIds[1][2]=R.drawable.fly3;
		mResourceIds[1][3]=R.drawable.fly4;
		
		mTextureNames[2][ARRAY_LENGTH+1]=1;
		mResourceIds[2][0]=R.drawable.skate2;
		
		mTextureNames[4][ARRAY_LENGTH+1]=5;
		mResourceIds[4][0]=R.drawable.bomb;
		mResourceIds[4][1]=R.drawable.bomb;
		mResourceIds[4][2]=R.drawable.bomb;
		mResourceIds[4][3]=R.drawable.bomb;
		mResourceIds[4][4]=R.drawable.bomb;
		
		width=128;
		height=128;
		cwidth=40;
		cheight=20;
	}

	@Override
	public void update(int timer) {
		super.update(timer);
		
		if(x<=480&&x>=400){
			velocityX = -20;
		}else if(x<400&&x>=300){
			if(x%50==0){
				attack();
			}
			velocityX = -5;
		}else if(x<300&&x>=0){
			velocityX = -20;
		}
		switch (state) {
		case STATE_ATTACK:
//			if (mTextureNames[state][ARRAY_LENGTH+2] + timer > 200) {
//				state = STATE_MOVE;
//			}
//			velocityX = 0;
			break;
		case STATE_MOVE:
//			velocityX = -20;
			break;
		case STATE_DEAD:
			velocityX = -5;
			if (mTextureNames[state][ARRAY_LENGTH] + timer > 90) {
				isDead=true;
			}
			break;
		default:
			break;
		}
		
		AnimaUpdate(timer);
	}
	
	@Override
	protected void move(int timer) {
		x += timer * velocityX/100;
		y += timer * velocityY/100;
	}

	private void AnimaUpdate(int timer) {
		if (mTextureNames[state][ARRAY_LENGTH] + timer >= mTextureNames[state][ARRAY_LENGTH+1] * 40)
			mTextureNames[state][ARRAY_LENGTH]= 0;
		mTextureNames[state][ARRAY_LENGTH] += timer;
		setTextureName(mTextureNames[state][mTextureNames[state][ARRAY_LENGTH] / 40]);
	}
	

	private void attack() {
		mScene.addBullets(mScene.mSpritesRes[4],-30,0,x+width/2-cwidth,y+height/2-10,24,24,TYPE_SPRI_BULLET);
	}

	public void underAttack() {
		hp-=55;//dead immediately
		if (hp <= 0) {
			state = STATE_DEAD;
		}
	}
	@Override
	public boolean collisionWith(GLSprite something) {
		if(state==STATE_DEAD)
			return false;
		switch(something.type){
		case GLSprite.TYPE_PLAYER_BULLET:
			something.isDead=true;
			underAttack();
			return true;
		}
		return false;
	}
	
	@Override
	public void setup() {
		// TODO Auto-generated method stub
		super.setup();
		hp=100;
		mTextureNames[STATE_DEAD][ARRAY_LENGTH]=0;
	}
}
